<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      @submit.native.prevent
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        :label="`${$majorName ? $majorName : '专业'}名称`"
        prop="majorName"
      >
        <el-input
          v-input-restrict
          v-model="queryParams.majorName"
          :placeholder="`请输入${$majorName ? $majorName : '专业'}名称`"
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div>
      <div></div>
    </div>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form @submit.native.prevent@submit.native.prevent ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item
          :label="`${$branchName ? $branchName : '分院'}名称`"
          prop=""
        >
          {{ $route.query.branchName }}
        </el-form-item>
        <el-form-item
          :label="`${$majorName ? $majorName : '专业'}名称`"
          prop="majorName"
        >
          <el-input
          v-input-restrict
            style="width: 80%"
            v-model="form.majorName"
            :placeholder="`请输入${$majorName ? $majorName : '专业'}名称`"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>

        <el-form-item
          :label="`${$majorName ? $majorName : '专业'}简介`"
          prop="content"
        >
          <el-input
          v-input-restrict
            style="width: 80%"
            maxlength="500"
            show-word-limit
            :rows="4"
            v-model="form.content"
            type="textarea"
            :placeholder="`请输入${$majorName ? $majorName : '专业'}简介`"
          />
        </el-form-item>

        <el-form-item label="封面" prop="majorPic">
          <div class="flex">
            <!-- 上传图片 -->
            <div
              v-if="form.majorPic"
              class="upload-head"
              @click="editCropper()"
            >
              <img v-bind:src="form.majorPic" class="cover" />
            </div>
            <div v-else class="upload-head" @click="editCropper()">
              <div>
                <img src="@/assets/images/add.png" class="add" alt="" />
              </div>
              <div>上传图片</div>
            </div>

            <div class="uploadTip">
              建议图片比例16:10，支持jpg、png格式，图片小于5M
            </div>
          </div>

          <vuecut
            :w="320"
            :h="200"
            :url="form.majorPic"
            @coverResult="coverResult"
            title="图片上传"
            :open.sync="vuecutshow"
          ></vuecut>
        </el-form-item>
<!--        <el-form-item label="课程设置方案" prop="majorPic">-->
<!--          <div class="flex">-->
<!--            &lt;!&ndash;     做题状态可点击上传        &ndash;&gt;-->
<!--            <uploadFile-->
<!--              :type="3"-->
<!--              :echo-file="echoFile"-->
<!--              :accept="'.doc,.docx,.pdf'"-->
<!--              :max-image-size="10"-->
<!--              :max-video-size="300"-->
<!--              @uploadFiles="uploadFiles"-->
<!--            ></uploadFile>-->
<!--            <div class="uploadTip">-->
<!--              支持word、pdf格式，文件小于50M-->
<!--            </div>-->
<!--          </div>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="open = false">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
          >确 定</el-button
        >

      </div>
    </el-dialog>
    <!-- 列表 -->
    <div v-infinite-scroll="getMore" class="list flex">
      <el-empty
        v-if="postList.length == 0"
        style="margin: 0 auto"
        :description="`暂无${$majorName ? $majorName : '专业'}~`"
      ></el-empty>
      <div
        :style="{
          backgroundImage: `url('${item.majorPic?item.majorPic:require('@/assets/images/major.png')}')`,
          backgroundSize: 'cover',
          backgroundRepeat: 'no-repeat',
        }"
        class="card rela shou"
        v-for="(item, index) in postList"
        :key="index"
        @click="toMajor(item)"
      >
        <div class="function abso">
          <div class="iconbox" @click.stop="handleUpdate(item)">
            <img class="shou" src="@/assets/images/icon2.png" />
          </div>
          <div class="iconbox" @click.stop="handleDelete(item)">
            <img class="shou" src="@/assets/images/icon3.png" />
          </div>
        </div>
        <div class="bottom abso">
          <div class="content" style="white-space: pre-wrap">
            {{ item.content }}
          </div>
          <div class="t">{{ item.majorName }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  listPost_major,
  getPost_major,
  delPost_major,
  addPost_major,
  updatePost_major,
} from "@/api/schoolOrganization/organization";
import uploadFile from "@/components/uploadFile/uploadFile";
export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  components:{ uploadFile },
  data() {
    return {
      echoFile:[],
      vuecutshow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总页数
      totalPage: 0,
      // 岗位表格数据
      postList: [],
      upLoadData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // level
      level: [],
      // 查询参数
      activeIndex: "",
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        majorName: undefined,
        branchId: undefined,
      },
      // 表单参数
      form: {
        majorId: undefined,
        branchId: undefined,
        majorName: undefined,
        majorPic: undefined,
        content: undefined,
      },
      // 表单校验
      rules: {
        majorName: [
          {
            required: true,
            message: `${
              this.$majorName ? this.$majorName : "专业"
            }名称不能为空`,
            trigger: "blur",
          },
        ],
        // content: [{ required: true, message: "简介不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getMore() {
      this.upLoadData = [];
      if (this.postList.length >= this.total) {
        console.log("没有更多");
      } else {
        this.queryParams.pageNum++;
        if (this.queryParams.pageNum > this.totalPage) {
          console.log("没有更多了");
          return;
        }
        this.getList(true);
      }
    },
    //上传视频||图片
    uploadFiles(e ) {
      console.log(e)
    },
    /* 去专业管理页面 */
    toMajor(row) {
      this.$router.push({
        path: "/schoolOrganization/class",
        query: {
          majorId: row.majorId,
          branchId: this.$route.query.branchId,
          branchName: this.$route.query.branchName,
          majorName: row.majorName,
        },
      });
    },
    /** 组件返回图片 */
    coverResult(e) {
      this.form.majorPic = e;
    },
    editCropper() {
      this.vuecutshow = true;
    },
    // 状态选择 查询列表
    handleSelect(key, keyPath) {
      this.queryParams.status = key;
      this.getList();
    },
    /** 查询岗位列表 */
    getList(num) {
      if (!num) {
        this.queryParams = {
          pageNum: 1,
          pageSize: 12,
        };
        this.postList = [];
      }
      this.loading = true;
      this.queryParams.branchId = this.$route.query.branchId;
      listPost_major(this.queryParams).then((response) => {
        if (num) {
          this.upLoadData = response.rows;
          this.postList = this.postList.concat(this.upLoadData);
        } else {
          this.postList = response.rows;
        }
        this.total = response.total;
        //向上取整 总页数
        this.totalPage = Math.ceil(response.total / this.queryParams.pageSize);
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        majorId: undefined,
        branchId: undefined,
        majorName: undefined,
        majorPic: undefined,
        content: undefined,
      };
      this.resetForm("form");
    },
    /*  管理员设置*/
    HandleAdmin() {},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = `添加${this.$majorName ? this.$majorName : "专业"}`;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.majorId || this.ids;
      getPost_major(postId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = `修改${this.$majorName ? this.$majorName : "专业"}`;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.branchId = this.$route.query.branchId;
          if (this.form.majorId != undefined) {
            updatePost_major(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost_major(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const majorId = row.majorId || this.ids;
      this.$modal
        .confirm("确定删除？")
        .then(function () {
          return delPost_major(majorId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
// 上传图片样式
.uploadTip {
  margin-left: 5px;
  font-size: 12px;
  font-weight: 400;
  color: rgba(166, 166, 166, 1);
  line-height: 210px;
  height: 150px;
}

.upload-head {
  cursor: pointer;
  position: relative;
  display: inline-block;
  width: 172px;
  height: 112px;
  border-radius: 4px;
  border: 1px dashed rgba(184, 196, 209, 1);

  .add {
    width: 29px;
    height: 29px;
    margin: 25px auto 0 auto;
  }

  div {
    font-size: 14px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    margin-top: 0px;
    width: 100%;
    text-align: center;
  }

  .cover {
    width: 170px;
    height: 110px;
    border-radius: 4px;
  }
}
.list {
  max-height: calc(100vh - 220px);
  overflow-y: scroll;
  margin-top: 16px;

  .card:hover {
    .function {
      display: flex;
    }

    .bottom {
      .content {
        display: -webkit-box;
      }
    }
  }

  .card {
    overflow: hidden;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 340px;
    height: 220px;
    border-radius: 4px;
    margin: 0 40px 40px 0;

    .function {
      display: none;
      border-radius: 0px 4px 0px 4px;
      padding: 0 8px 0 8px;
      right: 0;
      top: 0;
      border-radius: 0px 4px, 0px, 4px;
      background: rgba(0, 0, 0, 0.3);
     .iconbox{
        padding: 5px 6px 5px 6px;
     }
      img {
        width: 16px;
        height: 16px;
      }
    }

    .bottom {
      bottom: 0;

      .content {
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: none; //特别显示模式
        -webkit-line-clamp:8; //行数
        line-clamp: 8;
        -webkit-box-orient: vertical; //盒子中内容竖直排列
        width: 340px;
        box-sizing: border-box;
        background: rgba(0, 0, 0, 0.3);
        padding: 2px 12px;
        font-size: 10px;
        font-weight: 400;
        color: rgba(255, 255, 255, 1);
      }

      .t {
        text-align: center;
        font-size: 16px;
        font-weight: 400;
        line-height: 40px;
        color: rgba(255, 255, 255, 1);
        width: 340px;
        height: 40px;
        border-radius: 0px 0px, 4px, 4px;
        background: rgba(35, 184, 112, 1);
      }
    }
  }
}
</style>
